package com.lfxfs.config;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalTimeSerializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.format.Formatter;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

/**
 * jackson序列化配置类
 *
 * @author liubo
 * @date 2020-03-30
 */
@Configuration
public class JacksonConfiguration {

    private String DATA_FORMAT_A ="yyyy-MM-dd HH:mm:ss";

    private String DATA_FORMAT_B ="yyyy-MM-dd";

    private String DATA_FORMAT_C ="HH:mm:ss";



    @Bean(name = "mapperObject")
    public ObjectMapper objectMapper(){
        ObjectMapper objectMapper = new ObjectMapper();
        DateFormat dateFormat = new SimpleDateFormat(DATA_FORMAT_A);
        JavaTimeModule javaTimeModule = new JavaTimeModule();
        javaTimeModule.addSerializer(LocalDateTime.class,new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(DATA_FORMAT_A)));
        javaTimeModule.addSerializer(LocalDate.class,new LocalDateSerializer(DateTimeFormatter.ofPattern(DATA_FORMAT_B)));
        javaTimeModule.addSerializer(LocalTime.class,new LocalTimeSerializer(DateTimeFormatter.ofPattern(DATA_FORMAT_C)));
        javaTimeModule.addDeserializer(LocalDateTime.class,new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern(DATA_FORMAT_A)));
        javaTimeModule.addDeserializer(LocalDate.class,new LocalDateDeserializer(DateTimeFormatter.ofPattern(DATA_FORMAT_B)));
        javaTimeModule.addDeserializer(LocalTime.class,new LocalTimeDeserializer(DateTimeFormatter.ofPattern(DATA_FORMAT_C)));
        objectMapper.registerModule(javaTimeModule)
                .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,false)
                .configure(DeserializationFeature.ACCEPT_EMPTY_ARRAY_AS_NULL_OBJECT,true)
                //属性为空不序列化
                .setDefaultPropertyInclusion(JsonInclude.Include.ALWAYS)
                .setDateFormat(dateFormat);
        return objectMapper;
    }

    @Bean
    public Formatter<LocalDate> localDateFormatter(){
        return new Formatter<LocalDate>(){

            @Override
            public String print(LocalDate localDate, Locale locale) {
                return localDate.format(DateTimeFormatter.ofPattern(DATA_FORMAT_B));
            }

            @Override
            public LocalDate parse(String s, Locale locale) throws ParseException {
                return LocalDate.parse(s,DateTimeFormatter.ofPattern(DATA_FORMAT_B));
            }
        };
    }

    @Bean
    public Formatter<LocalTime> localTimeFormatter(){
        return new Formatter<LocalTime>() {
            @Override
            public LocalTime parse(String s, Locale locale) throws ParseException {
                return LocalTime.parse(s,DateTimeFormatter.ofPattern(DATA_FORMAT_C));
            }

            @Override
            public String print(LocalTime localTime, Locale locale) {
                return localTime.format(DateTimeFormatter.ofPattern(DATA_FORMAT_C));
            }
        };
    }


    @Bean
    public Formatter<LocalDateTime> localDateTimeFormatter(){
        return new Formatter<LocalDateTime>() {
            @Override
            public LocalDateTime parse(String s, Locale locale) throws ParseException {
                return LocalDateTime.parse(s,DateTimeFormatter.ofPattern(DATA_FORMAT_A));
            }

            @Override
            public String print(LocalDateTime localDateTime, Locale locale) {
                return localDateTime.format(DateTimeFormatter.ofPattern(DATA_FORMAT_A));
            }
        };
    }


}
